package com.backend.mzyf.controllers.api;


import com.backend.mzyf.Utils.JsonUtil;
import com.backend.mzyf.Utils.ReturnUtil;
import com.backend.mzyf.Utils.UploadUtil;
import com.backend.mzyf.Utils.excelUtils.ExcelRead;
import com.backend.mzyf.controllers.BaseController;
import com.backend.mzyf.entity.ActiveUser;
import com.backend.mzyf.entity.SysUser;
import com.backend.mzyf.service.SysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api")
public class ApiController extends BaseController {

    @Autowired
    private SysService sysService;

    @ResponseBody
    @RequestMapping(value = "/login",method = {RequestMethod.POST})
    public HashMap<String, Object> login(HttpSession session, HttpServletRequest request) throws Exception {

        String usercode = request.getParameter("usercode");
        String password = request.getParameter("password");

        ActiveUser activeUser = sysService.authenticat(usercode,password);

        session.setAttribute("activeUser",activeUser);

        return ReturnUtil.returnSuccess("login success");
    }

    @ResponseBody
    @RequestMapping(value = "/register",method = {RequestMethod.POST})
    public HashMap<String,Object> register(@RequestBody SysUser user) throws Exception {
        System.out.println(user);
        String userJson = JsonUtil.obj2json(user);
        System.out.println(userJson);
        return ReturnUtil.returnSuccess(userJson);
    }

    @ResponseBody
    @RequestMapping(value = "/readExcel",method = {RequestMethod.POST})
    public Map<String, Object> readExcel(@RequestParam(value="excelFile") MultipartFile file,
                         HttpServletRequest request, HttpServletResponse response, HttpSession session)
            throws IOException {
        if (file == null){
            return ReturnUtil.returnFaild("file id empty！");
        }
        try {
            String fileName = file.getOriginalFilename();
            Long size = file.getSize();

            List<ArrayList<String>> list = new ExcelRead().readExcel(file);

            System.out.println(fileName);
            System.out.println(size);
            System.out.println(list);
            Boolean result = UploadUtil.saveFile(file);
            System.out.println(result);
            if (result) {
                return ReturnUtil.returnSuccess("upload success");
            }else{
                return ReturnUtil.returnFaild("upload failed");
            }
        }catch (Exception e){
            return ReturnUtil.returnFaild("upload failed");
        }


    }

}
